<?php

namespace App\Repositories;

use App\Enums\GiftOpTypeEnum;
use App\Enums\GiftTypeEnum;
use App\Exceptions\BasicException;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use function Matrix\add;

//引入hash

class RecommendRepository
{

    /*房间推荐列表*/
    public function getRoomList($page, $perPage)
    {
        $recommendListModel = app('model_recommend_list');
        $userRoomModel = app("model_user_room");
        $list = $recommendListModel
            ->from($recommendListModel->getTable() . " as t1")
            ->selectRaw('t1.id,t1.priority,t1.recom_num,t1.into_room_num,t1.create_time,t2.room_id as room_code,t2.room_name')
            ->Join(DB::raw($userRoomModel->getTable() . " as t2"), "t1.recom_id", "=", "t2.id")
            ->where(['t1.recom_type' => 1, 't1.is_deleted' => 0])
            ->orderBy("t1.id", "desc")
            ->paginate($perPage, ['*'], '', $page);
        foreach ($list as $item) {
            $item->create_time = date('Y-m-d H:i:s', $item->create_time);
        }

        return $list;
    }

    /*搜索房间*/
    public function roomSearch($param)
    {
        $userRoomRes = app("model_user_room")->selectRaw('id as room_id,room_name')->where(['room_id' => $param['room_code']])->first();
        if (!$userRoomRes) {
            throw new BasicException(99001);
        }

        return $userRoomRes;
    }

    /*新增房间推荐*/
    public function roomAdd($param)
    {
        $recommendListHave = app("model_recommend_list")->where(['recom_id' => $param['room_id'], 'recom_type' => 1, 'is_deleted' => 0])->first();
        if ($recommendListHave) {
            throw new BasicException(99002);
        }

        $recommendListDel = app("model_recommend_list")->where(['recom_id' => $param['room_id'], 'recom_type' => 1, 'is_deleted' => 1])->first();
        if ($recommendListDel) {
            $userRoomRes = app("model_recommend_list")->where('id', $recommendListDel['id'])->update([
                'is_deleted' => 0,
                'priority' => $param['priority'],
                'update_time' => time(),
            ]);
            return $userRoomRes;
        }
        $userRoomRes = app("model_user_room")->where(['id' => $param['room_id']])->first();
        if (!$userRoomRes) {
            throw new BasicException(99001);
        }

        $userRoomRes = app("model_recommend_list")->insert([
            'recom_id' => $param['room_id'],
            'recom_type' => 1,
            'priority' => $param['priority'],
            'create_time' => time(),
        ]);

        return $userRoomRes;
    }

    /*编辑房间推荐数据*/
    public function roomEditDetail($param)
    {
        $recommendListModel = app('model_recommend_list');
        $userRoomModel = app("model_user_room");
        $recommendRers = $recommendListModel
            ->from($recommendListModel->getTable() . " as t1")
            ->selectRaw('t1.id,t1.priority,t2.room_id as room_code,t2.room_name')
            ->Join(DB::raw($userRoomModel->getTable() . " as t2"), "t1.recom_id", "=", "t2.id")
            ->where(['t1.id' => $param['id'], 't1.recom_type' => 1, 't1.is_deleted' => 0])
            ->first();

        return $recommendRers;
    }

    /*编辑房间推荐数据*/
    public function roomEdit($param)
    {
        $recommendListHave = app("model_recommend_list")->where([['id', '!=', $param['id']], 'recom_id' => $param['room_id'], 'recom_type' => 1, 'is_deleted' => 0])->first();
        if ($recommendListHave) {
            throw new BasicException(99005);
        }

        $recommendListHave = app("model_recommend_list")->where(['id' => $param['id'], 'recom_type' => 1, 'is_deleted' => 0])->first();
        if (!$recommendListHave) {
            throw new BasicException(99003);
        }
        $userRoomRes = app("model_user_room")->where(['id' => $param['room_id']])->first();
        if (!$userRoomRes) {
            throw new BasicException(99001);
        }

        $userRoomRes = app("model_recommend_list")->where('id', $param['id'])->update([
            'recom_id' => $param['room_id'],
            'priority' => $param['priority'],
            'update_time' => time(),
        ]);

        return $userRoomRes;
    }

    /*删除房间推荐*/
    public function roomDel($param)
    {
        $recommendListHave = app("model_recommend_list")->where(['id' => $param['id'], 'recom_type' => 1, 'is_deleted' => 0])->first();
        if (!$recommendListHave) {
            throw new BasicException(99003);
        }
        $recommendDel = app("model_recommend_list")->where(['id' => $param['id'], 'recom_type' => 1, 'is_deleted' => 0])->update([
            'is_deleted' => 1,
        ]);
        return $recommendDel;
    }

    /*主播推荐列表*/
    public function getUserList($page, $perPage)
    {
        $recommendListModel = app('model_recommend_list');
        $userDataModel = app("model_user_data");
        $list = $recommendListModel
            ->from($recommendListModel->getTable() . " as t1")
            ->selectRaw('t1.id,t1.priority,t1.recom_num,t1.into_room_num,t1.create_time,t2.user_code,t2.nickname')
            ->Join(DB::raw($userDataModel->getTable() . " as t2"), "t1.recom_id", "=", "t2.user_id")
            ->where(['t1.recom_type' => 2, 't1.is_deleted' => 0])
            ->orderBy("t1.id", "desc")
            ->paginate($perPage, ['*'], '', $page);
        foreach ($list as $item) {
            $item->create_time = date('Y-m-d H:i:s', $item->create_time);
        }

        return $list;
    }

    /*搜索主播*/
    public function userSearch($param)
    {
        $userRoomRes = app("model_user_data")->selectRaw('user_id,nickname')->where(['user_code' => $param['user_code']])->first();
        if (!$userRoomRes) {
            throw new BasicException(99004);
        }

        return $userRoomRes;
    }

    /*新增主播推荐*/
    public function userAdd($param)
    {
        $recommendListHave = app("model_recommend_list")->where(['recom_id' => $param['user_id'], 'recom_type' => 2, 'is_deleted' => 0])->first();
        if ($recommendListHave) {
            throw new BasicException(99005);
        }

        $recommendListDel = app("model_recommend_list")->where(['recom_id' => $param['user_id'], 'recom_type' => 2, 'is_deleted' => 1])->first();
        if ($recommendListDel) {
            $userRoomRes = app("model_recommend_list")->where('id', $recommendListDel['id'])->update([
                'is_deleted' => 0,
                'priority' => $param['priority'],
                'update_time' => time(),
            ]);
            return $userRoomRes;
        }
        $userRoomRes = app("model_user_data")->where(['user_id' => $param['user_id']])->first();
        if (!$userRoomRes) {
            throw new BasicException(99004);
        }

        $userRoomRes = app("model_recommend_list")->insert([
            'recom_id' => $param['user_id'],
            'recom_type' => 2,
            'priority' => $param['priority'],
            'create_time' => time(),
        ]);

        return $userRoomRes;
    }

    /*编辑主播推荐数据*/
    public function userEditDetail($param)
    {
        $recommendListModel = app('model_recommend_list');
        $userRoomModel = app("model_user_data");
        $recommendRers = $recommendListModel
            ->from($recommendListModel->getTable() . " as t1")
            ->selectRaw('t1.id,t1.priority,t2.user_code,t2.nickname')
            ->Join(DB::raw($userRoomModel->getTable() . " as t2"), "t1.recom_id", "=", "t2.user_id")
            ->where(['t1.id' => $param['id'], 't1.recom_type' => 2, 't1.is_deleted' => 0])
            ->first();

        return $recommendRers;
    }

    /*编辑主播推荐数据*/
    public function userEdit($param)
    {
        $recommendListHave = app("model_recommend_list")->where([['id', '!=', $param['id']], 'recom_id' => $param['user_id'], 'recom_type' => 2, 'is_deleted' => 0])->first();
        if ($recommendListHave) {
            throw new BasicException(99005);
        }
        $recommendListHave = app("model_recommend_list")->where(['id' => $param['id'], 'recom_type' => 2, 'is_deleted' => 0])->first();
        if (!$recommendListHave) {
            throw new BasicException(99003);
        }
        $userRoomRes = app("model_user_data")->where(['user_id' => $param['user_id']])->first();
        if (!$userRoomRes) {
            throw new BasicException(99004);
        }

        $userRoomRes = app("model_recommend_list")->where('id', $param['id'])->update([
            'recom_id' => $param['user_id'],
            'priority' => $param['priority'],
            'update_time' => time(),
        ]);

        return $userRoomRes;
    }

    /*删除房间推荐*/
    public function userDel($param)
    {
        $recommendListHave = app("model_recommend_list")->where(['id' => $param['id'], 'recom_type' => 2, 'is_deleted' => 0])->first();
        if (!$recommendListHave) {
            throw new BasicException(99003);
        }
        $recommendDel = app("model_recommend_list")->where(['id' => $param['id'], 'recom_type' => 2, 'is_deleted' => 0])->update([
            'is_deleted' => 1,
        ]);
        return $recommendDel;
    }
}
